#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>

std::string changeId(const pthread_t& pthread_id)
{
    char tid[128];
    snprintf(tid,sizeof(tid),"0x%x",pthread_id);
    return tid;
}

void *start_routine(void *args)
{
    std::string threadname = static_cast<const char *>(args); // 安全的强制类型转换
    pthread_detach(pthread_self());
    while (true)
    {
        std::cout << threadname << " running ...: " << changeId(pthread_self()) << std::endl;
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, start_routine, (void *)"thread 1");
    std::string main_id=changeId(pthread_self());

    std::cout << "main thread running ...: " << changeId(tid) << std::endl;

    return 0;
}